/**
 * 邮件模板相关的API服务
 */

import { request } from '@umijs/max';

// 模板类型枚举
export enum TemplateType {
  HTML = 'html',
  MARKDOWN = 'markdown',
  PLAIN_TEXT = 'plain_text',
}

// 模板分类枚举
export enum TemplateCategory {
  MARKETING = 'marketing',
  NOTIFICATION = 'notification',
  NEWSLETTER = 'newsletter',
  WELCOME = 'welcome',
  REMINDER = 'reminder',
  CUSTOM = 'custom',
}

// 邮件模板接口定义
export interface EmailTemplateCreate {
  name: string;
  subject: string;
  content: string;
  template_type: TemplateType;
  category: TemplateCategory;
  description?: string;
  variables?: string[];
  is_active: boolean;
}

export interface EmailTemplateUpdate {
  name?: string;
  subject?: string;
  content?: string;
  template_type?: TemplateType;
  category?: TemplateCategory;
  description?: string;
  variables?: string[];
  is_active?: boolean;
}

export interface EmailTemplateItem {
  id: number;
  name: string;
  subject: string;
  content: string;
  template_type: TemplateType;
  category: TemplateCategory;
  description?: string;
  variables: string[];
  is_active: boolean;
  user_id: number;
  created_at: string;
  updated_at: string;
}

export interface EmailTemplateList {
  items: EmailTemplateItem[];
  total: number;
  page: number;
  size: number;
  pages: number;
}

export interface TemplatePreviewRequest {
  template_id?: number;
  subject?: string;
  content?: string;
  template_type: TemplateType;
  variables?: Record<string, any>;
}

export interface TemplatePreviewResponse {
  rendered_subject: string;
  rendered_content: string;
  html_content: string;
  variables_used: string[];
  missing_variables: string[];
}

export interface TemplateVariableExtractRequest {
  content: string;
  subject?: string;
}

export interface TemplateVariableExtractResponse {
  variables: string[];
  subject_variables: string[];
  content_variables: string[];
}

export interface TemplateStatistics {
  total: number;
  active: number;
  by_category: Record<string, number>;
  by_type: Record<string, number>;
  recent_added: number;
}

export interface TemplateCloneRequest {
  template_id: number;
  new_name: string;
}

// API服务函数
export async function getEmailTemplates(params: {
  page?: number;
  size?: number;
  search?: string;
  category?: TemplateCategory;
  template_type?: TemplateType;
  active_only?: boolean;
}): Promise<EmailTemplateList> {
  return request('/api/templates/', {
    method: 'GET',
    params,
  });
}

export async function getEmailTemplate(id: number): Promise<EmailTemplateItem> {
  return request(`/api/templates/${id}`, {
    method: 'GET',
  });
}

export async function createEmailTemplate(data: EmailTemplateCreate): Promise<EmailTemplateItem> {
  return request('/api/templates/', {
    method: 'POST',
    data,
  });
}

export async function updateEmailTemplate(id: number, data: EmailTemplateUpdate): Promise<EmailTemplateItem> {
  return request(`/api/templates/${id}`, {
    method: 'PUT',
    data,
  });
}

export async function deleteEmailTemplate(id: number): Promise<{ message: string }> {
  return request(`/api/templates/${id}`, {
    method: 'DELETE',
  });
}

export async function toggleTemplateStatus(id: number): Promise<EmailTemplateItem> {
  return request(`/api/templates/${id}/toggle`, {
    method: 'POST',
  });
}

export async function previewTemplate(data: TemplatePreviewRequest): Promise<TemplatePreviewResponse> {
  return request('/api/templates/preview', {
    method: 'POST',
    data,
  });
}

export async function extractTemplateVariables(data: TemplateVariableExtractRequest): Promise<TemplateVariableExtractResponse> {
  return request('/api/templates/extract-variables', {
    method: 'POST',
    data,
  });
}

export async function cloneTemplate(data: TemplateCloneRequest): Promise<EmailTemplateItem> {
  return request('/api/templates/clone', {
    method: 'POST',
    data,
  });
}

export async function getTemplateStatistics(): Promise<TemplateStatistics> {
  return request('/api/templates/statistics', {
    method: 'GET',
  });
}

export async function getActiveTemplates(): Promise<EmailTemplateItem[]> {
  return request('/api/templates/active/list', {
    method: 'GET',
  });
}

export async function searchTemplatesAutocomplete(params: {
  q: string;
  limit?: number;
}): Promise<EmailTemplateItem[]> {
  return request('/api/templates/search/autocomplete', {
    method: 'GET',
    params,
  });
}
